﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EyFramework.Common;
using EyFramework.Library;
using EyFramework.Sys.Core.Service;
using EyFramework.Sys.Entity.Dto;
using EyFramework.Task.Entity;
using EyFramework.ThirdPart.RedisUtil;

namespace EyFramework.Task
{
    public partial class FrmTaskManager : FrmDevBase
    {
        private TaskCard currentTaskCard = null;
        private IQrtzCsTaskService qrtzCsTaskService;

        public FrmTaskManager()
        {
            InitializeComponent();
        }

        private void FrmTaskManager_Load(object sender, EventArgs e)
        {
            qrtzCsTaskService = Globals.GetService<IQrtzCsTaskService>();
            QueryData();
            RefreshTaskService();
        }

        private void QueryData()
        {
            TaskQueryParam taskQueryParam = new TaskQueryParam();
            var taskList = qrtzCsTaskService.GetAllTask(taskQueryParam);
            taskList.ForEach(p =>
            {
                var taskCard = new TaskCard(p);
                flpMain.Controls.Add(taskCard);
            });
            flpMain.Controls.Add(new TaskCard());
        }

        private void TaskCard_Click(object sender, EventArgs e)
        {
            if (currentTaskCard != null)
            {
                currentTaskCard.BorderStyle = BorderStyle.None;
            }
            TaskCard tsCard = (TaskCard)sender;
            currentTaskCard = tsCard;
            tsCard.BorderStyle = BorderStyle.FixedSingle;
        }

        private void btnStartService_Click(object sender, EventArgs e)
        {
            ServiceController cs1 = new ServiceController();
            cs1.MachineName = "localhost";
            cs1.ServiceName = "XseyScheduler";  // 服务名称
            try
            {
                if (cs1.Status == ServiceControllerStatus.Stopped
                    || cs1.Status == ServiceControllerStatus.StopPending)
                {
                    cs1.Start();
                    cs1.Refresh();
                    RefreshTaskService();
                }
            }
            catch (Exception ex)
            {
                Globals.ShowError("服务启动失败,请手工后台服务启动！失败原因:" + ex.Message);
            }
        }

        private void btnStopService_Click(object sender, EventArgs e)
        {
            ServiceController cs1 = new ServiceController();
            cs1.MachineName = "localhost";
            cs1.ServiceName = "XseyScheduler";  // 服务名称
            try
            {
                if (cs1.Status == ServiceControllerStatus.Running)
                {
                    cs1.Stop();
                    cs1.Refresh();
                    RefreshTaskService();
                }
            }
            catch (Exception ex)
            {
                Globals.ShowError("服务停止失败,请手工后台服务停止！失败原因:" + ex.Message);
            }
        }

        public void RefreshTaskService()
        {
            WinTaskServiceState winTaskServiceState = RedisHelper.Get<WinTaskServiceState>(TaskConstants.WINSERVER_REDIS_KEY);
            if (winTaskServiceState != null)
            {
                lblHostName.Text = winTaskServiceState.HostName;
                lblSchelState.Text = winTaskServiceState.RunState;
                lblSchelState.BackColor = Color.Green;
                lblHostIp.Text = winTaskServiceState.HostIp;
                lblStartTime.Text = winTaskServiceState.StartTime;
            }
            else
            {
                lblHostName.Text = "";
                lblSchelState.Text = "未运行";
                lblSchelState.BackColor = Color.Red;
                lblHostIp.Text = "";
                lblStartTime.Text = "";
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshTaskService();
        }
    }
}